
//
//  HWTopicsMoreViewController.m
//  HappyWeekend
//
//  Created by 赵洁 on 16/1/13.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWTopicsMoreViewController.h"
#import "HWTopicsMoreCollectionViewCell.h"
#import "HWTopicsMoreModel.h"
#import "HWTopicsDetiallViewController.h"
#import <MJRefresh.h>
@interface HWTopicsMoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)CGFloat requsetLimt;
@end

@implementation HWTopicsMoreViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = _string;
    self.dataArray = [NSMutableArray array];
    [self addCollectionView];
    [self setUpData];
    [self setUpchannelData];
   
}

- (void)setUpchannelData
{
    NSString *str = [NSString stringWithFormat:@"http://api.guozhoumoapp.com/v2/channels/%@/items?limit=20&offset=0&gender=2&generation=1",_CModel.Id];
    [HWRequestManager requestWithUrl:str parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *itemsArray = dataDic[@"items"];
        
        for (NSDictionary*oneDic in itemsArray) {
            HWTopicsMoreModel *MModel = [[HWTopicsMoreModel alloc]init];
            [MModel setValuesForKeysWithDictionary:oneDic];
            [_dataArray addObject:MModel];
        }
        [_collectionView reloadData];
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
}

- (void)setUpData
{
    NSString *str = [NSString stringWithFormat:@"http://api.guozhoumoapp.com/v2/collections/%@/posts?limit=20&offset=0",_model.ID];
    NSLog(@"%@",str);
    
    [HWRequestManager requestWithUrl:str parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *postsArray = dataDic[@"posts"];
        self.dataArray = [NSMutableArray array];
        for (NSDictionary*oneDic in postsArray) {
            HWTopicsMoreModel *moreModel = [[HWTopicsMoreModel alloc]init];
            [moreModel setValuesForKeysWithDictionary:oneDic];
            [self.dataArray addObject:moreModel];
        }
        
        [_collectionView reloadData];
        
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
}
- (void)addCollectionView
{
    UICollectionViewFlowLayout *laout = [[UICollectionViewFlowLayout alloc]init];
    laout.scrollDirection = UICollectionViewScrollDirectionVertical;
    laout.minimumLineSpacing = 10;
    laout.minimumInteritemSpacing = 10;
    laout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    laout.itemSize = CGSizeMake((kScreenWidth-10), 130);
    _collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:laout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.alwaysBounceVertical = YES;//强制性竖直滑动
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[HWTopicsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.view addSubview:_collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HWTopicsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
    HWTopicsMoreModel *model = _dataArray[indexPath.row];
    cell.model= model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HWTopicsDetiallViewController *detiallVC = [[HWTopicsDetiallViewController alloc]init];
    HWTopicsMoreModel *model = _dataArray[indexPath.row];
    detiallVC.model = model;
    [self.navigationController pushViewController:detiallVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
